package com.rayc.mall.user.controller;

import com.rayc.mall.api.dto.LoginFormDTO;
import com.rayc.mall.api.dto.RegisterFormDTO;
import com.rayc.mall.api.dto.UserUpdateDTO;
import com.rayc.mall.core.annotation.CacheAddByUserId;
import com.rayc.mall.core.annotation.CacheRemoveByUserId;
import com.rayc.mall.common.constant.Constants;
import com.rayc.mall.common.context.UserContext;
import com.rayc.mall.common.result.Result;
import com.rayc.mall.common.utils.BeanUtils;
import com.rayc.mall.user.domain.po.User;
import com.rayc.mall.user.domain.vo.UserLoginVO;
import com.rayc.mall.user.domain.vo.UserVO;
import com.rayc.mall.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Ray-C
 * @CreateTime: 2025-03-25
 * @Description: 用户相关接口
 * @Version: 1.0
 */
@Slf4j
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@Validated LoginFormDTO loginFormDTO) {
        log.info("用户登录 {}", loginFormDTO);
        UserLoginVO userLoginVO = userService.login(loginFormDTO);
        log.info("用户登录成功 {}", userLoginVO);
        return Result.success(userLoginVO);
    }

    @ApiOperation("用户注册接口")
    @PostMapping("/register")
    public Result<String> register(@Validated RegisterFormDTO registerFormDTO) {
        log.info("用户注册 {}", registerFormDTO);
        userService.register(registerFormDTO);
        log.info("用户注册成功 {}", registerFormDTO);
        return Result.success();
    }

    @ApiOperation("更新用户信息接口")
    @PutMapping("/updateInfo")
    @CacheRemoveByUserId(keys = Constants.REDIS_KEY_USERS)
    public Result<String> updateInfo(@Validated UserUpdateDTO userUpdateDTO) {
        log.info("更新用户信息 {}", userUpdateDTO);
        userService.updateInfo(userUpdateDTO);
        log.info("用户信息更新成功 {}", userUpdateDTO);
        return Result.success();
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/userInfo")
    @CacheAddByUserId(key = Constants.REDIS_KEY_USERS)
    public Result<UserVO> queryUserInfo() {
        log.info("获取用户信息");
        User user = userService.getById(UserContext.getUser());
        UserVO userVO = BeanUtils.copyBean(user, UserVO.class);
        log.info("获取用户信息成功 {}", userVO);
        return Result.success(userVO);
    }

    // feign interface
    @PutMapping("/deduct")
    public Result<String> deductMoney(@RequestParam String password, @RequestParam Integer amount) {
        log.info("尝试扣减余额 user: {}, amount: {}", UserContext.getUser(), amount);
        userService.deductMoney(password, amount);
        log.info("扣减余额成功 user: {}, amount: {}", UserContext.getUser(), amount);
        return Result.success();
    }
}
